//    Copyright (c) 2009 Elwyn Malethan
//
//    This file is part of SeemoreJ.
//
//    SeemoreJ is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SeemoreJ is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with SeemoreJ.  If not, see <http://www.gnu.org/licenses/>.

package com.malethan.seemorej.taglib;

public class Util {
    //----------------------------------------------------------------------- Static Properties and Constants

    public static final int HIGHEST_ENTITY = '>';
    public static char[][] entityMap = new char[HIGHEST_ENTITY + 1][];
    static {
        entityMap['&'] = "&amp;".toCharArray();
        entityMap['<'] = "&lt;".toCharArray();
        entityMap['>'] = "&gt;".toCharArray();
        entityMap['"'] = "&#034;".toCharArray();
        entityMap['\''] = "&#039;".toCharArray();
    }


    //----------------------------------------------------------------------- Static Methods
    public static StringBuffer appendAttribute(StringBuffer html, String attr, String value) {
        if (value != null && value.length() > 0) {
            html.append(" ").append(attr).append("=\"").append(escapeXml(value)).append("\"");
        }
        return html;
    }

    /**
     * From Apache core taglibs
     * 
     * Performs the following substring replacements
     * (to facilitate output to XML/HTML pages):
     *
     *    & -> &amp;
     *    < -> &lt;
     *    > -> &gt;
     *    " -> &#034;
     *    ' -> &#039;
     *
     * See also org.apache.taglibs.standard.tag.common.core.Util.writeEscapedXml().
     */
    public static String escapeXml(String buffer) {
        int start = 0;
        int length = buffer.length();
        char[] arrayBuffer = buffer.toCharArray();
        StringBuffer escapedBuffer = null;

        for (int i = 0; i < length; i++) {
            char c = arrayBuffer[i];
            if (c <= HIGHEST_ENTITY) {
                char[] escaped = entityMap[c];
                if (escaped != null) {
                    // create StringBuffer to hold escaped xml string
                    if (start == 0) {
                        escapedBuffer = new StringBuffer(length + 5);
                    }
                    // add unescaped portion
                    if (start < i) {
                        escapedBuffer.append(arrayBuffer,start,i-start);
                    }
                    start = i + 1;
                    // add escaped xml
                    escapedBuffer.append(escaped);
                }
            }
        }
        // no xml escaping was necessary
        if (start == 0) {
            return buffer;
        }
        // add rest of unescaped portion
        if (start < length) {
            escapedBuffer.append(arrayBuffer,start,length-start);
        }
        return escapedBuffer.toString();
    }

    //----------------------------------------------------------------------- Instance Properties
    //----------------------------------------------------------------------- Constructors
    //----------------------------------------------------------------------- Getters and Setters
    //----------------------------------------------------------------------- Instance Methods
}
